package org.jeecg.modules.weixin.utill;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.modules.system.util.HttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public final class WXConstants {

    public static final String APP_ID = "wx514043b01156569f";//wxbbb175062ece83cd
    public static final String APP_SECRET = "3e7d0b90797cc4e75a66200016a9ded0";//30950c4759dc9ebad923dda73d9f4dd5
    public static final String REAL_NAME = "y88csv.natappfree.cc";//nwx.wtsms.net
    public static final String CODE = "";

//    public static final String APP_ID = "wxbbb175062ece83cd";
//    public static final String APP_SECRET = "30950c4759dc9ebad923dda73d9f4dd5";
//    public static final String REAL_NAME = "test.lzgtzh.com";
//    public static final String CODE = "";
    /***
     * 应用授权作用域，snsapi_base （不弹出授权页面，直接跳转，只能获取用户openid），
     * snsapi_userinfo （弹出授权页面，可通过openid拿到昵称、性别、所在地。并且， 即使在未关注的情况下，只要用户授权，也能获取其信息 ）
     */
    public static final String SNSAPI = "snsapi_base";//
    //授权
    public static final String AUTH_BASE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?";
    //获取token
    public static final String ACCESS_TOKEN_BASE_URL = "https://api.weixin.qq.com/sns/oauth2/access_token";
    //获取用户信息
    public static final String INFO_BASE_URL = "https://api.weixin.qq.com/sns/userinfo";
    //回调
    public static String REDIRECT_URL = "http://"+REAL_NAME+"/sell/weixin/auth";
    //允许的范围
    public static final String SCOPE = "snsapi_userinfo";
    //token
    public static final String TOKEN = "与上面设置的token一致";


    /**
     * 生成用于获取access_token的Code的Url
     * @param redirectUrl
     * @return
     */
    public static String getRequestCodeUrl(String redirectUrl) {
        return String.format("https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s"
                        +"&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect",
                APP_ID, redirectUrl, SNSAPI, "123_state");
    }

    /**
     * 生成获取请求用户信息的access_token的url
     * @param code
     * @return
     */
    public static String getRequestAccessTokenUrl(String code) {
        return  String.format("%s?appid=%s&secret=%s&code=%s&grant_type=authorization_code",
                ACCESS_TOKEN_BASE_URL,APP_ID, APP_SECRET, code);
    }

    /**
     * 获取请求用户信息的access_token
     *
     * @param code
     * @return
     */
    public static JSONObject getUserAccessTokenInfo(String code) {
        JSONObject jsonObject = new JSONObject();
        Map<String, String> data = new HashMap();
        try {
            String url = WXConstants.getRequestAccessTokenUrl(code);
            System.out.println("   getRequestAccessTokenUrl : "+url);
            String resStr = HttpRequest.sendGet(url,"");
            jsonObject = JSONObject.parseObject(resStr);
            System.out.println(" jsonObject :"+jsonObject);
            /**{
             "access_token":"ACCESS_TOKEN",
             "expires_in":7200,
             "refresh_token":"REFRESH_TOKEN",
             "openid":"OPENID",
             "scope":"SCOPE"
             }**/

        } catch (Exception ex) {
            System.out.println("fail to request wechat access token. error= "+ex.getMessage());
        }
        return jsonObject;
    }

    public static JSONObject  getWXUserInfo(String accessToken, String openId){
        String url = INFO_BASE_URL+"?access_token=" + accessToken + "&openid=" + openId + "&lang=zh_CN";
        System.out.println(" request user info from url: "+url);
        JSONObject jsonObject = null;
        try {
            String resStr = HttpRequest.sendGet(url,"");
            jsonObject = JSONObject.parseObject(resStr);
            System.out.println(" jsonObject :"+jsonObject);

//            data.put("openid", userInfo.get("openid").toString().replaceAll("\"", ""));
//            data.put("nickname", userInfo.get("nickname").toString().replaceAll("\"", ""));
//            data.put("city", userInfo.get("city").toString().replaceAll("\"", ""));
//            data.put("province", userInfo.get("province").toString().replaceAll("\"", ""));
//            data.put("country", userInfo.get("country").toString().replaceAll("\"", ""));
//            data.put("headimgurl", userInfo.get("headimgurl").toString().replaceAll("\"", ""));
        } catch (Exception ex) {
            System.out.println(" getWXUserInfo errror: "+ex.getMessage());
        }
        return jsonObject;
    }


}
